<template>
  <div class="side-navigation" :class="{ 'collapsed': collapsed }">
    <!-- Logo区域 -->
    <div class="logo-section">
      <div class="logo-container">
        <div class="logo-icon">
          <el-icon size="32" :color="themeColor">
            <DataAnalysis />
          </el-icon>
        </div>
        <div class="logo-text" v-show="!collapsed">
          <h3>ShopPuls</h3>
          <p>用户行为分析</p>
        </div>
      </div>
    </div>
    
    <!-- 导航菜单 -->
    <el-menu
      :default-active="activeMenu"
      class="navigation-menu"
      :collapse="collapsed"
      :unique-opened="true"
      router
      :background-color="backgroundColor"
      :text-color="textColor"
      :active-text-color="themeColor"
    >
      <!-- 概览仪表板 -->
      <el-menu-item index="/dashboard" class="menu-item">
        <el-icon><Odometer /></el-icon>
        <template #title>概览仪表板</template>
      </el-menu-item>
      
      <!-- 用户管理 -->
      <el-menu-item index="/users/manage" class="menu-item">
        <el-icon><User /></el-icon>
        <template #title>用户管理</template>
      </el-menu-item>
      
      <!-- 数据分析 -->
      <el-sub-menu index="analysis" class="sub-menu">
        <template #title>
          <el-icon><TrendCharts /></el-icon>
          <span>数据分析</span>
        </template>
        <el-menu-item index="/analysis/profile/basic">
          <el-icon><User /></el-icon>
          <template #title>基础画像</template>
        </el-menu-item>
        <el-menu-item index="/analysis/profile/behavior">
          <el-icon><Monitor /></el-icon>
          <template #title>行为画像</template>
        </el-menu-item>
        <el-menu-item index="/analysis/profile/consumption">
          <el-icon><Money /></el-icon>
          <template #title>消费画像</template>
        </el-menu-item>
        <el-menu-item index="/analysis/segmentation">
          <el-icon><Grid /></el-icon>
          <template #title>用户分群</template>
        </el-menu-item>
      </el-sub-menu>
      
      <!-- 预测分析 -->
      <el-sub-menu index="prediction" class="sub-menu">
        <template #title>
          <el-icon><MagicStick /></el-icon>
          <span>预测分析</span>
        </template>
        <el-menu-item index="/prediction/churn">
          <el-icon><Warning /></el-icon>
          <template #title>流失预测</template>
        </el-menu-item>
        <el-menu-item index="/prediction/purchase">
          <el-icon><ShoppingCart /></el-icon>
          <template #title>购买预测</template>
        </el-menu-item>
      </el-sub-menu>
      
      <!-- 推荐系统 -->
      <el-menu-item index="/recommendation" class="menu-item">
        <el-icon><Star /></el-icon>
        <template #title>推荐系统</template>
      </el-menu-item>
      
      <!-- 可视化大屏 -->
      <el-sub-menu index="datav" class="sub-menu">
        <template #title>
          <el-icon><Monitor /></el-icon>
          <span>可视化大屏</span>
        </template>
        <el-menu-item index="/datav/monitor">
          <el-icon><View /></el-icon>
          <template #title>实时监控</template>
        </el-menu-item>
        <el-menu-item index="/datav/user-analysis">
          <el-icon><UserFilled /></el-icon>
          <template #title>用户分析</template>
        </el-menu-item>
        <el-menu-item index="/datav/sales-analysis">
          <el-icon><TrendCharts /></el-icon>
          <template #title>销售分析</template>
        </el-menu-item>
        <el-menu-item index="/datav/operation">
          <el-icon><Setting /></el-icon>
          <template #title>运营决策</template>
        </el-menu-item>
      </el-sub-menu>

      <!-- 模型管理 -->
      <el-sub-menu index="models" class="sub-menu">
        <template #title>
          <el-icon><MagicStick /></el-icon>
          <span>模型管理</span>
        </template>
        <el-menu-item index="/models/browser">
          <el-icon><View /></el-icon>
          <template #title>模型浏览</template>
        </el-menu-item>
        <el-menu-item index="/models/train">
          <el-icon><VideoPlay /></el-icon>
          <template #title>模型训练</template>
        </el-menu-item>
        <el-menu-item index="/models/evaluate">
          <el-icon><DataAnalysis /></el-icon>
          <template #title>模型评估</template>
        </el-menu-item>
      </el-sub-menu>

      <!-- 系统管理 -->
      <el-sub-menu index="system" class="sub-menu">
        <template #title>
          <el-icon><Tools /></el-icon>
          <span>系统管理</span>
        </template>
        <el-menu-item index="/system/page-structure">
          <el-icon><Connection /></el-icon>
          <template #title>页面结构</template>
        </el-menu-item>
        <el-menu-item index="/system/monitor">
          <el-icon><Monitor /></el-icon>
          <template #title>系统监控</template>
        </el-menu-item>
        <el-menu-item index="/system/users">
          <el-icon><User /></el-icon>
          <template #title>用户管理</template>
        </el-menu-item>
        <el-menu-item index="/system/permissions">
          <el-icon><Key /></el-icon>
          <template #title>权限管理</template>
        </el-menu-item>
      </el-sub-menu>
    </el-menu>
  </div>
</template>

<script setup>
/* eslint-disable no-undef */
import { computed, ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import {
  DataAnalysis, Odometer, FolderOpened, Upload, Document, TrendCharts,
  User, Monitor, Money, Grid, MagicStick, Warning, ShoppingCart,
  Star, View, UserFilled, Setting, Tools, Connection, Key, VideoPlay
} from '@element-plus/icons-vue'

// Props
defineProps({
  collapsed: {
    type: Boolean,
    default: false
  }
})

// Emits
defineEmits(['toggle-collapse'])

const route = useRoute()

// 主题色相关
const themeColor = ref('#409EFF')
const backgroundColor = ref('#ffffff')
const textColor = ref('#333333')

// 获取当前主题色
const getCurrentTheme = () => {
  const root = document.documentElement
  const primaryColor = getComputedStyle(root).getPropertyValue('--theme-primary').trim()
  const bgColor = getComputedStyle(root).getPropertyValue('--bg-color').trim()
  const txtColor = getComputedStyle(root).getPropertyValue('--text-color-primary').trim()

  if (primaryColor) themeColor.value = primaryColor
  if (bgColor) backgroundColor.value = bgColor
  if (txtColor) textColor.value = txtColor
}

// 监听主题变化
const watchThemeChange = () => {
  const observer = new MutationObserver(() => {
    getCurrentTheme()
  })

  observer.observe(document.documentElement, {
    attributes: true,
    attributeFilter: ['style', 'class']
  })

  return observer
}

// 计算当前激活的菜单项
const activeMenu = computed(() => {
  return route.path
})

// 生命周期
onMounted(() => {
  getCurrentTheme()
  watchThemeChange()
})
</script>

<style scoped>
.side-navigation {
  width: 240px;
  height: 100vh;
  background-color: var(--bg-color, #ffffff);
  border-right: 1px solid var(--border-color, #e6e8eb);
  box-shadow: var(--shadow-medium, 2px 0 8px rgba(0, 0, 0, 0.1));
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  transition: width 0.3s ease;
  overflow: hidden;
}

.side-navigation.collapsed {
  width: 64px;
}

/* Logo区域 */
.logo-section {
  height: 64px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid var(--border-color, #e6e8eb);
  background: var(--bg-color, #ffffff);
}

.logo-container {
  display: flex;
  align-items: center;
  width: 100%;
}

.logo-icon {
  flex-shrink: 0;
  margin-right: 12px;
}

.logo-text {
  flex: 1;
  min-width: 0;
}

.logo-text h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--theme-primary, #409EFF);
  line-height: 1.2;
}

.logo-text p {
  margin: 0;
  font-size: 12px;
  color: var(--text-color-secondary, #666);
  line-height: 1.2;
}

/* 导航菜单 */
.navigation-menu {
  border: none;
  height: calc(100vh - 64px);
  overflow-y: auto;
}

.navigation-menu:not(.el-menu--collapse) {
  width: 240px;
}

/* 菜单项样式 */
.navigation-menu :deep(.el-menu-item) {
  height: 48px;
  line-height: 48px;
  margin: 4px 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.navigation-menu :deep(.el-menu-item:hover) {
  background-color: var(--theme-primary-alpha-10, rgba(64, 158, 255, 0.1)) !important;
  color: var(--theme-primary, #409EFF) !important;
}

.navigation-menu :deep(.el-menu-item.is-active) {
  background-color: var(--theme-primary, #409EFF) !important;
  color: #ffffff !important;
  font-weight: 500;
}

.navigation-menu :deep(.el-menu-item.is-active .el-icon) {
  color: #ffffff !important;
}

/* 子菜单样式 */
.navigation-menu :deep(.el-sub-menu) {
  margin: 4px 8px;
}

.navigation-menu :deep(.el-sub-menu__title) {
  height: 48px;
  line-height: 48px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.navigation-menu :deep(.el-sub-menu__title:hover) {
  background-color: var(--theme-primary-alpha-10, rgba(64, 158, 255, 0.1)) !important;
  color: var(--theme-primary, #409EFF) !important;
}

.navigation-menu :deep(.el-sub-menu.is-active .el-sub-menu__title) {
  color: var(--theme-primary, #409EFF) !important;
  font-weight: 500;
}

/* 子菜单项样式 */
.navigation-menu :deep(.el-menu--inline .el-menu-item) {
  margin: 2px 16px;
  padding-left: 40px !important;
  background-color: transparent;
}

.navigation-menu :deep(.el-menu--inline .el-menu-item:hover) {
  background-color: var(--theme-primary-alpha-10, rgba(64, 158, 255, 0.1)) !important;
}

.navigation-menu :deep(.el-menu--inline .el-menu-item.is-active) {
  background-color: var(--theme-primary, #409EFF) !important;
  color: #ffffff !important;
}

/* 图标样式 */
.navigation-menu :deep(.el-icon) {
  margin-right: 8px;
  font-size: 16px;
  transition: color 0.3s ease;
}

/* 折叠状态下的样式 */
.side-navigation.collapsed .navigation-menu :deep(.el-menu-item),
.side-navigation.collapsed .navigation-menu :deep(.el-sub-menu__title) {
  margin: 4px;
  justify-content: center;
}

.side-navigation.collapsed .navigation-menu :deep(.el-icon) {
  margin-right: 0;
}

/* 滚动条样式 */
.navigation-menu::-webkit-scrollbar {
  width: 4px;
}

.navigation-menu::-webkit-scrollbar-track {
  background: transparent;
}

.navigation-menu::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 2px;
}

.navigation-menu::-webkit-scrollbar-thumb:hover {
  background: #9ca3af;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .side-navigation {
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .side-navigation.mobile-open {
    transform: translateX(0);
  }
}
</style>
